package com.hsav.kcxb.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.adapter.SingerWorkAdapter;
import com.hsav.kcxb.main.MSYS;
import com.kcbsdk.AFetchType;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyRankBa.AObbligStyle;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.AIconType;

import java.util.ArrayList;
import java.util.List;

import main.MyDevice;

import static com.hsav.kcxb.main.MSYS.mMyRankBa;
import static com.hsav.kcxb.main.MSYS.mMyTools;

public class SingerWorkActivity extends AppCompatActivity {

    private static final String TAG = SingerWorkActivity.class.getName();
    private static final String ACTION_FIND_BY_SINGER_NAME = "com.hsav.kcxb.activity.action.find_by_singer_name";
    private static final String KEY_SINGER_NAME = "com.hsav.kcxb.activity.extra.key_singer_name";
    private static final String ACTION_FIND_BY_SONG_NAME = "com.hsav.kcxb.activity.action.find_by_song_name";
    private static final String KEY_SONG_NAME = "com.hsav.kcxb.activity.extra.key_song_name";
    private static final String ACTION_RECOMMEND = "com.hsav.kcxb.activity.action.recommand";
    private ListView mListView;
    private String action;
    private String singerName;
    private String songName;
    private ArrayList<AObbligatoList> songs;
    private AObbligStyle style = AObbligStyle.OBBLIG_RECOMMEND;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_work);
        initView();
        action = getIntent().getAction();
        if (ACTION_FIND_BY_SINGER_NAME.equals(action))
        {
            singerName = getIntent().getStringExtra(KEY_SINGER_NAME);
            style = AObbligStyle.OBBLIG_BY_ARTIST;
        }
        else if (ACTION_FIND_BY_SONG_NAME.equals(action))
        {
            songName = getIntent().getStringExtra(KEY_SONG_NAME);
            style = AObbligStyle.OBBLIG_BY_NAME;
        }
        else
        {
            style = AObbligStyle.OBBLIG_RECOMMEND;
        }
        initData();
    }

    public static void startWithFilterSingerName(Context context, String singerName)
    {
        Intent intent = new Intent(context, SingerWorkActivity.class);
        intent.setAction(ACTION_FIND_BY_SINGER_NAME);
        intent.putExtra(KEY_SINGER_NAME, singerName);
        context.startActivity(intent);
    }

    public static void startWithFilterSongName(Context context, String songName)
    {
        Intent intent = new Intent(context, SingerWorkActivity.class);
        intent.setAction(ACTION_FIND_BY_SONG_NAME);
        intent.putExtra(KEY_SONG_NAME, songName);
        context.startActivity(intent);
    }

    public static void startWithFilterRecommend(Context context)
    {
        Intent intent = new Intent(context, SingerWorkActivity.class);
        intent.setAction(ACTION_RECOMMEND);
        context.startActivity(intent);
    }

    private void initData() {
        mMyRankBa.setUiEventCallBack(this, new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0){
                    if (isFail){
                        mMyRankBa.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                    }
                    else {
                        mMyTools.Log(String.format("setUiEventCallBack %08x", methodType));

                        if (mMyRankBa.isSucessObbligatoListDbs(methodType, style)){
                            mMyTools.Log("isSucessObbligatoListDbs");
                            songs = new ArrayList<>();
                            for (int i = 0; i < mMyRankBa.mAObbligatoList.length; i++){
                                songs.add(mMyRankBa.mAObbligatoList[i]);
                            }
                            Log.d(TAG, String.format("歌曲数量：%s", songs.size()));
                            SingerWorkAdapter singerWorkAdapter = new SingerWorkAdapter(SingerWorkActivity.this);
                            mListView.setAdapter(singerWorkAdapter);
                        }
                    }
                }
                else {
                     if (mMyRankBa.isEventDownImgSmall()){
                        Log.d(TAG, "isEventDownImgSmall");
                        if (songs != null)
                        {
                            SingerWorkAdapter singerWorkAdapter = new SingerWorkAdapter(SingerWorkActivity.this);
                            mListView.setAdapter(singerWorkAdapter);
                        }
                    }
                }
            }
        });
        mMyTools.Log("singerName  " + style + " " +singerName);
    //    mMyTools.fetchIconTypeList(ACloudType.TYPE_KBA, AIconType.ICON_ARTIST_NAME, singerName);

//        if (ACTION_FIND_BY_SINGER_NAME.equals(action)){
            mMyRankBa.fetchObbligatoListDbs(AFetchType.LIST_FRESHEN, style, singerName);
//        }
//        else if (ACTION_FIND_BY_SONG_NAME.equals(action))
//        {
//            MSYS.mMyRankBa.fetchObbligatoListDbs(AFetchType.LIST_FRESHEN, style, 0, songName);
//        }
//        else
//        {
//            MSYS.mMyRankBa.fetchObbligatoListDbs(AFetchType.LIST_FRESHEN, style, 0, "");
//        }
    }

    private void initView() {
        ImageView leftImg = (ImageView)findViewById(R.id.iv_top_back);
        leftImg.setVisibility(View.VISIBLE);
        TextView titleTx = (TextView)findViewById(R.id.tv_top_title);
        String title = "推荐歌曲";
        if (ACTION_FIND_BY_SONG_NAME.equals(action))
        {
            title = songName;
        }
        else if (ACTION_FIND_BY_SINGER_NAME.equals(action))
        {
            title = singerName;
        }
        titleTx.setText(title);
        mListView = (ListView)findViewById(R.id.listview_work);
        mListView.setItemsCanFocus(true);
        mListView.setEmptyView(LayoutInflater.from(this).inflate(R.layout.layout_no_data, null, false));
    }
}
